var gulp   = require("gulp"),
path = require("path"),
livereload   = require("gulp-livereload"),
connect = require('gulp-connect'),
webpack = require('gulp-webpack'),
jshint = require('gulp-jshint');

var webpackConfig = require("./webpack.config.js");

gulp.task("watch", function () {
    gulp.watch("./dist/html/*", ['webpack', 'reload']);
    gulp.watch("./src/**/*", ['webpack']);
    gulp.watch("./dist/js/*", ['reload']);
})

gulp.task("reload", function () {
    gulp.src("./dist/**/*").pipe(connect.reload());
})

gulp.task("start-server", function () {
    connect.server({
        root: "./dist/",
        port: "8000",
        livereload: true
    });
})

gulp.task("watch-src", function () {
    gulp.watch("./src/**/*", ["webpack"]);
})

gulp.task("webpack", function (cb) {
    gulp.src("./src/js/**/*.js").pipe(jshint()).pipe(jshint.reporter('default'));
    gulp.src("./js")
        .pipe(webpack(webpackConfig))
        .pipe(gulp.dest("./dist/js"));
    //打包完成后再刷新
    cb();
})

gulp.task("default", ["webpack", "watch"]);